package encaps.security;

import encaps.action.Action;
import encaps.entities.Grant;
import encaps.entities.Role;
import encaps.utils.DataAccessException;
import encaps.utils.InitializationException;

public class SecuritySystem {

    private SecuritySystem() {
    }

    public static boolean isActionAllowed(String roleName, Action action) throws DataAccessException {
        Grant requiredGrant = action.getRequiredGrant();
        if (requiredGrant == null) {
            return true;
        }
        Role userRole = getRole(roleName);
        return userRole.hasGrant(requiredGrant);
    }

    public static Role getRole(String roleName) throws DataAccessException {
        try {
            return RoleManager.getRoleManager().getRole(roleName);
        } catch (InitializationException ex) {
            throw new DataAccessException(ex.getMessage(), ex);
        }
    }
}
